#ifndef KCENTERGREEDYINT8_H
#define KCENTERGREEDYINT8_H

#include <QObject>
#include <QVector>
#include <QDebug>
#include <random>
#include <limits>
#include <vector>
#include <cstdint>
#include <QWriteLocker>
#include <QThread>

class KCenterGreedyInt8 : public QObject {
    Q_OBJECT

public:
    static KCenterGreedyInt8& instance();


    // 增加新的数据到知识库
    void addToKnowledgeBase(const QVector<int8_t>& newData);

    int addEmbedding(void *data, size_t num);

    QVector<int8_t> embedding;

    void writeKnowledgeBase(QVector<int8_t> &value);
    QVector<int8_t> &readKnowledgeBase();

    int startBuildKnowledgeBase() ;
signals:
    void trianStartSignal();
    void trianStartProcess(int process);
    void trianStartFinish();

public slots:
    int buildKnowledgeBase();

private:
    explicit KCenterGreedyInt8(QObject* parent = nullptr);
    ~KCenterGreedyInt8();

    QReadWriteLock lock_;

    QVector<int8_t> knowledgeBase_;
    void updateDistances(size_t centerIdx);
    size_t getNewIdx();
    int32_t computeDistanceSquared(const int8_t* a, const int8_t* b) const;
    size_t totalPoints;
    //QVector<int8_t> knowledgeBase_; // 固定大小的知识库 (N_*D_)
    QVector<int32_t> minDistances_;
    static constexpr size_t N_ = 2000; // 知识库大小固定
    static constexpr size_t D_ = 1536; // 每个向量的维度固定
    int onceN_ = 784;

    QThread workerThread; // 工作线程
};

#endif // KCENTERGREEDYINT8_H
